#!/usr/bin/perl 

use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi();
print_header();

my $form_ref = parse_input();
my %FORM_DATA = %$form_ref;

my $supNum = $FORM_DATA{'supNum'};
my $supName = $FORM_DATA{'supName'};
my $supJob = $FORM_DATA{'supJob'};
my $supLoc = $FORM_DATA{'supLoc'};
my $empNum = $FORM_DATA{'empNum'};
my $payDate = $FORM_DATA{'payDate'};
my $empFac = $FORM_DATA{'empFac'};
my @jbs=();
my @tmp=();
my @jLst=();
my $head_count = get_employee_count ($empNum);
if ( $head_count == 1 ) { # this is good - one match found in employees table
	my $array_ref = get_employee_info ($empNum);
	my ($num, $first, $last, $job, $loc, $fac) = @$array_ref;
	my $empName = "$first" . " $last";
	if ( $empFac == 0 ) { $empFac = $fac };
 	my $pay_count = get_payroll_count ($empNum, $payDate);
	if ( $pay_count == 0 ) {
		create_payroll_event ($empNum, $payDate);
	}
	# create an empty temp time card table for the supervisor
	create_tmp_tc_table ($supNum);
	# see if there are job events for the employee
	my $jobCount = count_job_events ($empNum, $payDate);
#	print "JOB COUNT: $jobCount CREATING TABLE<br/>";
	# This creates a blank table called tc_tmp_[supervisors number]	
#	print "create_tmp_tc_table ($supNum, $empNum, $payDate, $jobCount)<br/>";
# 	create_tmp_tc_table ($supNum, $empNum, $payDate, $jobCount);
#	print "JOB COUNT: $jobCount CREATING TABLE<br/>";
	if ( $jobCount == 0 ) {	
		fill_tmp_tc_clean ($supNum,$empNum, $payDate);
		draw_clean_time_card_page ($supNum, $supName,$supJob,$supLoc,$empNum,$empName,$empFac,$payDate,$jobCount);
	} else {
		my $minMaxRef = get_time_card_start_end_sec();
		my ($min, $max ) = @$minMaxRef;
		my $inOut = get_punch_in_out ($empNum, $payDate );
		my ($in, $out) = @$inOut;	
#		print "OLA $min, $max<br/>";
		my $jLst_ref = get_job_events ($empNum, $payDate );
		my @jLst = @$jLst_ref;
		my ($Min,$Max, $Diff);
		my @tmp=();
#		while ($min < $jLst[0][6]) {
		while ($min < $in) {
			if ( $min+1350 > $in) {
				$Max = $in;
				$Diff = $Max-$min;
			} else {
				$Max = $min + 900;
				$Diff = 900;		
			}	
			@tmp = ($empNum, $payDate, "NULL", "NULL", "NULL", $min, $Max, $Diff, "TEMP");			
			push @jbs, [@tmp];
#			print "$empNum, $payDate, NULL, NULL, NULL, $min, $Max, $Diff, MAN<br/>";
			$min = $Max;
			$Max += 900;
		}
		for (my $i=0,; $i<=$#jLst; $i++) {
			my $empNum =  $jLst[$i][0];
			my $payDate = $jLst[$i][1];
			my $jobNum = $jLst[$i][2];
#			my $taskNum = $jLst[$i][3];
			my $docNum = $jLst[$i][3];
			my $cliID = $jLst[$i][4];
			my $start = $jLst[$i][5];
			my $end = $jLst[$i][6];
#			my $diff = $jLst[$i][8];
			my $source = $jLst[$i][7];
			my $End = $end;
			my $diff = $end-$start;
#			if ( $diff > 1350 ) { print "SHOULD MAKE A SPLIT! $docNum, $cliID, $start, $end, $diff, $source <br/>"; };
			while ( $diff > 1350 ) {
				$End = $start + 900;
				@tmp = ($empNum, $payDate, $jobNum, $docNum, $cliID, $start, $End, 900, "SYS");			
				push @jbs, [@tmp];
#				print "MAKING UP  $empNum, $payDate, $jobNum, $taskNum, $docNum, $cliID, $start, $End, 900, IN<br/>";			
				$start = $End;
				$diff = $end - $start;
			}	
			@tmp = ($empNum, $payDate, $jobNum, $docNum, $cliID, $start, $end, $diff, $source);			
			push @jbs, [@tmp];
#			print "$empNum, $payDate, $jobNum, $taskNum, $docNum, $cliID, $start, $end, $diff, $source<br/>";
			$Min = $end;
		}
		$Min = $out;
		if ($Min < $max ) {
			my $mod = $Min%900;
			my $fact = ($Min-($Min%900))/900;
			if ( $mod > 450 ) {
				$Max = ($fact+2)*900;
			} else {
				$Max = ($fact+1)*900;
			}
			$Diff=$Max-$Min;
			@tmp = ($empNum, $payDate, "NULL", "NULL", "NULL", $Min, $Max, $Diff, "TEMP");			
			push @jbs, [@tmp];
#			print "$empNum, $payDate, NULL, NULL, NULL, NULL, $Min, $Max, $Diff, MAN<br/>";
			$min=$Max;
			$Max += 900;
			while ($Max <= $max) {
				@tmp = ($empNum, $payDate, "NULL", "NULL", "NULL", $min, $Max, 900, "TEMP");			
				push @jbs, [@tmp];
#				print "$empNum, $payDate, NULL, NULL, NULL, NULL, $min, $Max, 900, MAN<br/>";
				$min = $Max;
				$Max += 900;
			}			
		}
	fill_tmp_tc_dirty ( \@jbs, $supNum);
#	die "Alan says put your hands on your head";
	draw_dirty_time_card_page ($supNum, $supName, $supJob, $supLoc, $empNum, $empName, $empFac, $payDate, $jobCount);
	}
} elsif ( $head_count == 0 ) {
   # no employee number found print a new log-in screen
   my $msg = "Employee ID $empNum not found in Data Base\!";	
	sup_activity_screen_from_pm ($supNum, $supName,$supJob, $supLoc, $msg);
} else {
   # we have a serious problem - more than one employee number found!
  trak_01_emp_log_in_more_than_one_number();  
}

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub draw_dirty_time_card_page
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob = $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $empName = $_[5];
	my $empFac = $_[6];
	my $payDate = $_[7];
	my $jobCount = $_[8];
	my @tcData = ();
	my $tcDataRef = get_time_card_data ($supNum);
	@tcData = @$tcDataRef;
	my $client_ref = get_time_card_clients ($empFac); 
	my @clients = @$client_ref;
  print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_time_card_data</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Time Card: $empName : $payDate : Facility # $empFac</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_04_sup_time_card_refresh.pl' method='post'>
  <input type="hidden" name="supNum" value='$supNum'/>
  <input type="hidden" name="supName" value='$supName'/>
  <input type="hidden" name="supJob" value='$supJob'/>
  <input type="hidden" name="supLoc" value='$supLoc'/>
  <input type="hidden" name="empNum" value='$empNum'/>
  <input type="hidden" name="empName" value='$empName'/>
  <input type="hidden" name="empFac" value='$empFac'/>
  <input type="hidden" name="payDate" value='$payDate'/>
  <input type="hidden" name="cleanDirty" value='DIRTY'/>
  
  <table id="small">
	<thead>
    <tr>
      <th>Time</th>
      <th>Doc No. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
      <th>Client</th>
    </tr>
    </thead>
	 <tbody>	
ENDHEADER
# <tbody class="scrollContent">	
 
 # print "THERE ARE $#tasks TASKS!<br/>"; 
 for my $i (0..$#tcData ) {
		my $id = $tcData[$i][0];		
		my $tag = $tcData[$i][1];	
		my $date = $tcData[$i][2];
		my $num = $tcData[$i][3];
		my $start = $tcData[$i][4];
		my $end = $tcData[$i][5];
		my $doc = $tcData[$i][6];
		my $cli = $tcData[$i][7];
		my $ti = $i;
#		if ( $cli && $cli ne "") {
#			print "$id, $tag, $date, $num, $start, $end, $doc, $cli <br/>";
#		}
		print <<ENDHEADER;
		<tr>
			<td align=left>$tag &nbsp&nbsp</td>
ENDHEADER
			if ($doc && $doc ne "" ) {
				print "<td align=left><input type='text' size='9' value='$doc' name='DOC_$id' /></td>";
			} else { 
				print "<td align=left><input type='text' size='9' name='DOC_$id' /></td>";
			}
			print "<td> <select id=clientDrop name='CLI_$id' tabindex='2' >";
			# add a blank element at the start of the list
			print "<option value='NUL'></option>";
			# add all clients on the list to the drop down
			foreach my $Cli (@clients) {
				if ( $cli && $cli ne "" && $Cli eq $cli ) {
		  			print "<option selected = 'selected' value=$Cli >$Cli</option>";
		  		} else {
		  			print "<option value=$Cli >$Cli</option>";
		  		}	
			}
			print <<ENDHEADER;
			</td>
		</tr>
ENDHEADER
 }
 print <<ENDHEADER;
 </tbody>
 </table>
 <br/>	
 <input type='submit' value='Refresh Time Card Page'>
 <br/><br/>
 <a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a>
 <br/><br/>
 </form>
  </div>	

 </body>
 </html>
ENDHEADER

}



sub draw_clean_time_card_page
{
	my $supNum = $_[0];
	my $supName = $_[1];
	my $supJob = $_[2];
	my $supLoc = $_[3];
	my $empNum = $_[4];
	my $empName = $_[5];
	my $empFac = $_[6];
	my $payDate = $_[7];
	my $jobCount = $_[8];
	my @tcData = ();
	my $tcData = get_time_card_data ($supNum);
	@tcData = @$tcData;
	my $client_ref = get_time_card_clients ($empFac); 
	my @clients = @$client_ref;

	# add a blank element at the start of the list
# 	my $optStr = "\<option value='NUL'\>\<\/option\>";
#	print "<option value='NUL'></option>";
	# add all clients on the list to the drop down
#	foreach my $cli (@clients) {
 #  	$optStr = $optStr . "\<option value\=$cli \>$cli\<\/option\>";
#	}
#	print "here is the option list $optStr<br/>";


  print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_time_card_data</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Time Card: $empName : $payDate : Facility # $empFac</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_04_sup_time_card_refresh.pl' method='post'>
  <input type="hidden" name="supNum" value='$supNum'/>
  <input type="hidden" name="supName" value='$supName'/>
  <input type="hidden" name="supJob" value='$supJob'/>
  <input type="hidden" name="supLoc" value='$supLoc'/>
  <input type="hidden" name="empNum" value='$empNum'/>
  <input type="hidden" name="empName" value='$empName'/>
  <input type="hidden" name="empFac" value='$empFac'/>
  <input type="hidden" name="payDate" value='$payDate'/>
  <input type="hidden" name="cleanDirty" value='CLEAN'/>
  <table id="small">
	<thead>
    <tr>
      <th>Time</th>
      <th>Doc No. &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</th>
      <th>Client</th>
    </tr>
    </thead>
	 <tbody>	
ENDHEADER
# <tbody class="scrollContent">	
 
 # print "THERE ARE $#tasks TASKS!<br/>"; 
 for my $i (0..$#tcData ) {
		my $id = $tcData[$i][0];		
		my $tag = $tcData[$i][1];	
		my $date = $tcData[$i][2];
		my $num = $tcData[$i][3];
		my $start = $tcData[$i][4];
		my $end = $tcData[$i][5];
		my $cli = $tcData[$i][6];
		my $ti = $i;
		if ( $cli eq "" ) {
#			print "$id, $tag, $date, $num, $start, $end, $cli <br/>";
			print <<ENDHEADER;
			<tr>
				<td align=left>$tag &nbsp&nbsp</td>
				<td align=left><input type="text" size="9" name="DOC_$id" /></td>
				<td> <select id=clientDrop name="CLI_$id" tabindex="2" >
ENDHEADER
#			print "$optStr";
			# add a blank element at the start of the list
			print "<option value='NUL'></option>";
			# add all clients on the list to the drop down
			foreach my $cli (@clients) {
	  			print "<option value=$cli >$cli</option>";
			}
			print <<ENDHEADER;
				</td>
			</tr>
ENDHEADER
		} else {
			print <<ENDHEADER;
			<tr>
				<td align=left>$tag &nbsp&nbsp</td>
				<td id="info" >$cli</td>
				<td><input type="hidden" name="DOC_$id" value="-1" /><input type="hidden" name="CLI_$id" value="logged_in" /></td> 
			</tr>
ENDHEADER
#			print "$id, $tag, $date, $num, $start, $end, $cli <br/>";
#			<td><input type='text' value='$cli' size='12' name='new_std_${id}' disabled="disabled" /></td>

		}	
			
 }
 print <<ENDHEADER;
 </tbody>
 </table>
 <br/>	
 <input type='submit' value='Refresh Time Card Page'>
 <br/><br/>
 <a href="${cgi_ref}trak_00_sup_activity.pl?num=$supNum&name=$supName&job=$supJob&loc=$supLoc">Return to Activities</a>
 </form>
 </div>	
 </body>
 </html>
ENDHEADER
}



